import Qs from 'qs'
import axios from 'axios'
import { baseURL } from './config.js'
import {getCookie,getStore,setStore} from '../commonUtil/mUtils'
import loading from "../components/loading";//全局加载

// ---------------------从APP 获取token-----------------
// let token = getCookie('token')!='undefined'?getCookie('token'):'';
var token = ''
var ua = navigator.userAgent;

function isWeixinBrowser(uaToLowerCase) {
  return (/micromessenger/.test(uaToLowerCase)) ? true : false;
}
function isApp(ua) {
  return ua.indexOf('web_ua')>-1?true:false;
}
var isiOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端


if (ua.indexOf('web_ua')>-1){
  var ua_data = JSON.parse(ua.match(/web_ua=(\S*)/)[1]);
  token = ua_data.token
  setStore('token',ua_data.token)
}else if(isWeixinBrowser(ua.toLowerCase())){
  token = getStore('token')
}else if(ua.indexOf('ios_app')>-1&&ua.indexOf('token')>-1){
  var ios_data = JSON.parse(ua.match(/ios_app=([\S\s]*)/)[1]);
  token = ios_data.token
  setStore('token',token)
}else {
  token = getStore('token')
  token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvYXBpLmhvbmdqaXRpbWVzLmNvbVwvaW50ZXJmYWNlXC9sb2dpbiIsImlhdCI6MTYwMzEwMDU1MywiZXhwIjoxNjA1NjkyNTUzLCJuYmYiOjE2MDMxMDA1NTMsImp0aSI6Inh2Z0NpVDdSaEl3UzBEWmEiLCJzdWIiOjcsInBydiI6ImY2YjcxNTQ5ZGI4YzJjNDJiNzU4MjdhYTQ0ZjAyYjdlZTUyOWQyNGQifQ.2uNnTByKGmz4yTqd_qJsgOAGFEePLK86MAYjwpl6jJo'
}

// --------------------------------获取token 结束---------------------

// 请求头
const headers = {
  'Content-Type':'application/x-www-form-urlencoded',
  'Authorization': 'Bearer ' + token,
  'token': token
}
// 路由
// axios.defaults.baseURL = baseURL;
//  请求拦截器；
axios.interceptors.request.use(
  config => {
    config.headers = headers;
    config.timeout = 10000;
    config.data = JSON.stringify(config.data);
    return config;
  },
  error => {
    return Promise.reject(err);
  }
);
//  响应拦截器；
axios.interceptors.response.use(
  response => {
    if(response.data.code*1 == 300 || response.data.code*1 == 401 ||response.data.code*1 == 498){
      //window.location.href = "https://fexpert2.hongjitimes.com/relogin"//去登陆
    }
    return response;
  },
  error => {
    return Promise.reject(error)
  }
)
export default {
  post(url,data={},apiLoading = false){
    apiLoading ? loading() : '' //如果启用apiloading则加载loading
    return new Promise((resolve,reject) => {
      axios.post(url, data)
        .then(response => {
          resolve(response);
        })
        .catch(err => {
          if (err && err.response.status == 500) {
            // console.log('接口500...')
          }
          reject(err)
        })
    })
  },
  get(url,params={},apiLoading = false){
    apiLoading ? loading() : '' //如果启用apiloading则加载loading
    return new Promise((resolve,reject) => {
      axios.get(url,{
        params:params
      })
        .then(response => {
          resolve(response);
        })
        .catch(err => {
          if (err && err.response.status == 500) {
            // console.log('接口500...')
          }
          reject(err)
        })
    })
  }
}
